package com.appspot.bugua;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import android.media.MediaPlayer;

public class MP3player {
	private static MP3player instance;
	public  final static String HOME="/sdcard/bugua";
	private  MediaPlayer mp ;
	public static MP3player instance (){
		if(instance==null){
			instance=new MP3player();
		}
		return instance;
	}
	private MP3player(){
		File home = new File(HOME);
		if(!home.exists())
			home.mkdir();
		if(mp==null){
			mp=new MediaPlayer();
		}
	}
    public void play(CharSequence fileName){
        
    	mp.reset();
        try {
			mp.setDataSource("/sdcard/bugua/"+fileName);
	        mp.prepare();
	        mp.start();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    public List<String> contentList(){
    	File home = new File(HOME);
    	List<String> data = new ArrayList<String>();
    	if(home.isDirectory()){
        
    	    for( String item : 	 home.list(new FilenameFilter() {
										@Override
										public boolean accept(File dir, String filename) {
											 
										    boolean b =Pattern.matches(".*mp3$", filename);
											return b;
											
										}
										}) ){
    	    	data.add(item);
    	    }
    	}

		
		
		return data;
	}
    
    public void release(){
    	mp.release();
    }
}
